package com.woniuxy.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.exception.BuildingInsertException;
import com.woniuxy.mapper.BuildingMapper;
import com.woniuxy.model.Building;
import com.woniuxy.service.BuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BuildingServiceImpl implements BuildingService {

    @Autowired
    private BuildingMapper buildingMapper;
    @Override
    public void addBuild(Building building) {
        int insert = buildingMapper.insert(building);
        if(insert!=1){
            throw new BuildingInsertException("新增失败");
        }
    }

    @Override
    public void deleteById(Integer id) {
        buildingMapper.deleteById(id);
    }

    @Override
    public void updateById(Building building) {
        buildingMapper.updateById(building);
    }

    @Override
    public List<Building> selectAll() {
        List<Building> buildings = buildingMapper.selectList(new QueryWrapper<Building>());
        return buildings;
    }

    @Override
    public List<Building> pageQuery(Integer page, Integer size,String remarks) {
        QueryWrapper<Building> queryWrapper = new QueryWrapper<>();
        //匹配楼栋进行查找
        queryWrapper.like(remarks!=null&&!remarks.trim().isEmpty(),"remarks",remarks);
        List<Building> buildings = buildingMapper.selectList(new Page<>(page, size), queryWrapper);
        return buildings;
    }
}
